package vip.specimen.system.controller.authority;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import vip.specimen.system.common.annotation.SystemJurisdiction;
import vip.specimen.system.common.enums.ResultCode;
import vip.specimen.system.datasource.entity.SystemRoleEntity;
import vip.specimen.system.datasource.validation.SystemRoleValidation;
import vip.specimen.system.result.JsonResult;
import vip.specimen.system.service.SystemRoleService;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

/**
 * *@Author: 余浪人
 * *@Date：2021/3/22 14:22
 * *@Email:yulangren520@Gmail.com
 * *@QQ:1368419640
 **/
@Api(tags = "角色管理")
@RestController
@RequestMapping(value = "/authority/role")
public class RoleController {
    @Resource
    private SystemRoleService roleService;

    @PutMapping
    @RequiresPermissions(value = "authority:role:insert")
    @ApiOperation(value = "新增", notes = "根据实体添加角色")
    @SystemJurisdiction(resourceType = "button", permissionName = "添加角色")
    public JsonResult<SystemRoleEntity> insert(@RequestBody @Valid SystemRoleValidation roleValidation) {
        return JsonResult.success(this.roleService.insert(roleValidation));
    }


    @GetMapping
    @RequiresPermissions(value = "authority:role:select")
    @ApiOperation(value = "查看", notes = "获取角色列表")
    @SystemJurisdiction(resourceType = "button", permissionName = "查看角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "每页记录条数", dataType = "int")
    })
    public JsonResult<Page<SystemRoleEntity>> findAll() {
        return JsonResult.success(this.roleService.findAll());
    }

    @GetMapping(value = "all")
    @RequiresPermissions(value = "authority:role:all")
    @ApiOperation(value = "查看全部", notes = "获取全部角色")
    @SystemJurisdiction(resourceType = "button", permissionName = "查看全部角色")
    public JsonResult<List<SystemRoleEntity>> findRoleAll() {
        return JsonResult.success(this.roleService.findRoleAll());
    }

    @PostMapping()
    @RequiresPermissions(value = "authority:role:update")
    @ApiOperation(value = "更新", notes = "根据uuid更新角色")
    @SystemJurisdiction(resourceType = "button", permissionName = "更新角色")
    public JsonResult<SystemRoleEntity> update(@RequestParam("uuid") UUID uuid, @RequestBody @Valid SystemRoleValidation roleValidation) {
        SystemRoleEntity result = this.roleService.update(uuid, roleValidation);
        return null == result ? JsonResult.failed(ResultCode.ERROR_SQL_UPDATE_SERVER) : JsonResult.success(result);
    }

    @DeleteMapping
    @RequiresPermissions(value = "authority:role:delete")
    @ApiOperation(value = "删除", notes = "根据uuid删除角色")
    @SystemJurisdiction(resourceType = "button", permissionName = "删除角色")
    public JsonResult delete(@RequestBody HashMap<String, UUID> param) {
        return this.roleService.delete(param.get("uuid")) ? JsonResult.success() : JsonResult.failed(ResultCode.ERROR_DELETE);
    }
}
